//
//  DrawImageView.swift
//  L02DrawShapes
//
//  Created by liy on 2024/12/25.
//

import UIKit

class DrawImageView: UIView {
	var img = UIImage(named: "boar")?.cgImage
	
	override func draw(_ rect: CGRect) {
		let ct = UIGraphicsGetCurrentContext()
		guard let img else {return}
//保存绘制状态供以后使用
		ct?.saveGState()
		ct?.translateBy(x: 0, y: 400)
		ct?.scaleBy(x: 1, y: -1)
		ct?.draw(img, in: CGRect(x: 0, y: 0, width: 200, height: 200))
		
		ct?.restoreGState()
		ct?.stroke(CGRect(x: 50, y: 100, width: 100, height: 100))
	}

}


#if DEBUG

import SwiftUI

struct DrawImageViewRepresentable: UIViewRepresentable {
    typealias UIViewType = DrawImageView

    func makeUIView(context: Context) -> UIViewType {
			let v = DrawImageView()
			v.backgroundColor = .white
			return v
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        
    }
}

struct DrawImageView_Previews: PreviewProvider {
    static var previews: some View {
        DrawImageViewRepresentable()
    }
}

#endif
